package com.gxgeek.cloud.feign.client;

import com.gxgeek.cloud.feign.entity.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.websocket.server.PathParam;

/**
 * @author gaoxing
 * @create 2018-01-02 下午7:54
 */
//@FeignClient(name = "microservice-provider-user" /**,fallback = UserFeignClient.HystrixClientFallback.class**/)
@FeignClient(name = "microservice-provider-user")
public interface UserFeignClient {
    /**
     * 这里有两个坑需要注意：<br/>
     *
     * <li>这里需要设置请求的方式为 RequestMapping 注解，用 GetMapping 注解是运行不成功的，即 GetMapping 不支持。</li>
     * <li>注解 PathVariable 里面需要填充变量的名字，不然也是运行不成功的。</li>
     *
     * @param id
     * @return
     */

    @RequestMapping(name = "/{id}",method = RequestMethod.GET)
//    public User findByIdFeign(@RequestParam("id") Long id);
//    public User findByIdFeign(@PathParam("id") Long id);
    public User findByIdFeign(@PathVariable("id") String  id);


    @Component
    static class HystrixClientFallback implements UserFeignClient {
        private static final Logger LOGGER = LoggerFactory.getLogger(HystrixClientFallback.class);

        /**
         * hystrix fallback方法
         * @param id id
         * @return 默认的用户
         */
        @Override
        public User findByIdFeign(String id) {
            HystrixClientFallback.LOGGER.info("异常发生，进入fallback方法，接收的参数：id = {}", id);
            User user = new User();
            user.setId(-1L);
            user.setUsername("default username");
            user.setAge(0);
            return user;
        }
    }

}
